﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain;
using Actuals.Common;
using Actuals.Business;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using System.Data;


namespace Actuals
{
    public partial class AddPartTimeDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               IBusinessEmployee businessEmployee = new BusinessEmployee();
               IList<IEmployee> employees = businessEmployee.GetEmployees();
               ddlEmployee.DataSource = employees;
               ddlEmployee.DataTextField = "NickName";
               ddlEmployee.DataValueField = "Id";
               ddlEmployee.DataBind();
               ddlEmployee.Items.Insert(0, "Select");


               IBusinessTeam businessTeam = new BusinessTeam();
               IList<ITeam> teams = businessTeam.GetTeams();
               ddlTeam.DataSource = teams;
               ddlTeam.DataTextField = "TeamDescription";
               ddlTeam.DataValueField = "TeamId";
               ddlTeam.DataBind();
               ddlTeam.Items.Insert(0, "Select");

            }
           
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IPartTimeDetails partTimeDetails = new PartTimeDetails();
                IBusinessPartTimeDetail businessPartTimeDetails = new BusinessPartTimeDetail();
                partTimeDetails.EmployeeId = int.Parse(ddlEmployee.SelectedValue);
                partTimeDetails.TeamId = int.Parse(ddlTeam.SelectedValue);
                partTimeDetails.NoOfHours = int.Parse(txtNoOfHours.Text.ToString());
                string[] _date1 = txtWorkFromDate.Text.Split('-');
                DateTime date1 = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));
                string[] _date2 = txtWorkToDate.Text.Split('-');
                DateTime date2 = new DateTime(int.Parse(_date2[2]), int.Parse(_date2[0]), int.Parse(_date2[1]));
                
              
                for (DateTime date = date1; date <= date2; date=(date.AddDays(1.0)))
                {
                    if ((businessPartTimeDetails.IsInValidDate(date, int.Parse(ddlEmployee.SelectedValue))) == false)
                    {
                        //string[] _workDate = date.Split('-');
                        partTimeDetails.WorkDate = date;
                            //new DateTime(int.Parse(_workDate[2]), int.Parse(_workDate[0]), int.Parse(_workDate[1]));
                        businessPartTimeDetails.AddPartTimeDetails(partTimeDetails);
                    }

                }
                
                
                
               
                lblMsg.Text = "Saved";
            }

            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }



    }
}